{
 "metadata": {
  "name": "ex_oscilloscope_waveform"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "InstrumentKit Library Examples"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Tektronix DPO 4104 Oscilloscope"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "In this example, we will demonstrate how to connect to a Tektronix DPO 4104 \n",
      "oscilloscope and transfer the waveform from channel 1 into memory."
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "We start by importing the InstrumentKit and numpy packages. In this example \n",
      "we require numpy because the waveforms will be returned as numpy arrays."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import instruments as ik\n",
      "import numpy as np"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Next, we open our connection to the oscilloscope. Here we use the associated\n",
      "TekDPO4104 class and open the connection via TCP/IP to address 192.168.0.2 on\n",
      "port 8080."
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "The connection method used will have to be changed to match your setup."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "tek = ik.tektronix.TekTDS224.open_tcpip('192.168.0.2', 8080)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Now that we are connected to the instrument, we can transfer the waveform \n",
      "from the oscilloscope. Note that Python channel[0] specifies the physical\n",
      "channel 1. This is due to Python's zero-based numbering vs Tektronix's\n",
      "one-based numbering."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "[x,y] = tek.channel[0].read_waveform()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "With the waveform now in memory, any other data analysis can be performed.\n",
      "Here we simply compute the mean value from the y-data."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print np.mean(y)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}